package me.sdevil507.drools.service;

/**
 * @ClassName RuleService
 * @Description: TODO
 * @Author wgc
 * @Date 2021/10/27 11:06
 * @Version V1.0
 **/
import me.sdevil507.drools.model.AddressCheckResult;
import me.sdevil507.drools.model.AddressRule;
import me.sdevil507.drools.model.Calculation;
import me.sdevil507.drools.model.CreditCardApplyInfo;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 调用规则引擎，执行规则
 */
@Service
public class RuleService {
    @Autowired
    private KieContainer kieContainer;
    @Autowired
    private KieBase kieBase;
    //个人所得税计算
    public Calculation calculate(Calculation calculation){
        KieSession kieSession = kieContainer.newKieSession();
        kieSession.insert(calculation);
        kieSession.fireAllRules();
        kieSession.dispose();
        return calculation;
    }

    //调用Drools规则引擎实现信用卡申请
    public CreditCardApplyInfo creditCardApply(CreditCardApplyInfo creditCardApplyInfo){
        KieSession session = kieBase.newKieSession();
        session.insert(creditCardApplyInfo);
        session.fireAllRules();
        session.dispose();
        return creditCardApplyInfo;
    }

    public AddressCheckResult calculateAmount(){
        KieSession session = kieBase.newKieSession();
        //设置Fact对象
        AddressRule addressRule = new AddressRule();
        addressRule.setTotalWeight(10);
        addressRule.setDistance(600);

        //将对象加入到工作内存
        session.insert(addressRule);

        AddressCheckResult addressCheckResult = new AddressCheckResult();
        session.insert(addressCheckResult);

        int i = session.fireAllRules();
        System.out.println("触发了" + i + "条规则");
        session.destroy();
        return addressCheckResult;
    }
}